package com.jiayinlending.jiayindai.fragment.mine;


import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.jiayinlending.jiayindai.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jiayinlending.jiayindai.R.id.bt_get_change;

/**
 * 验证新手机号
 */
public class ChangePhoneFragment extends Fragment {


    @Bind(bt_get_change)
    Button btGetChange;
    @Bind(R.id.bt_sub_change)
    Button btSubChange;
    private TimeCount time;
    public ChangePhoneFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_change_phone, container, false);
        ButterKnife.bind(this, rootView);
        //        构造CountDownTimer对象
        time = new TimeCount(60000, 1000);//构造CountDownTimer对象
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({bt_get_change, R.id.bt_sub_change})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case bt_get_change:
                time.start();
                btGetChange.setBackgroundColor(Color.parseColor("#CCCCCC"));
                break;
            case R.id.bt_sub_change:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.change_phone_container, new ChangePhoneVerificationFragment()).commit();
                break;
        }
    }
    /* 定义一个倒计时的内部类 */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {//计时完毕时触发
            btGetChange.setText("重新验证");
            btGetChange.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {//计时过程显示
            btGetChange.setClickable(false);
            btGetChange.setText(millisUntilFinished / 1000 + "秒");
        }
    }
}
